package com.robotserver.orders.jobs.xxljob;

import com.robotserver.orders.common.DateFormatUtil;
import com.robotserver.orders.common.DateUtil;
import com.robotserver.orders.constant.CacheKey;
import com.robotserver.orders.param.*;
import com.robotserver.orders.service.*;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class OrderPullByAppKeySyncXxlJob {
    private static Logger logger = LoggerFactory.getLogger(OrderPullByAppKeySyncXxlJob.class);

    @Autowired
    private JdOrderService jdOrderService;
    @Autowired
    private PddOrderService pddOrderService;
    @Autowired
    private TbOrderService tbOrderService;
    @Autowired
    private VipOrderService vipOrderService;
    @Autowired
    private VipRefundOrderService vipRefundOrderService;
    @Autowired
    private TbOrderRefundService tbOrderRefundService;
    @Autowired
    private ElmOrderService elmOrderService;
    @Autowired
    private DyOrderService dyOrderService;

    /**
     * 调用工具商拉取订单
     * 开始时间和结束时间，平台参数由界面参数传进来
     * 参数例子：type,beginTimeStr,endTimeStr
     * type：JD、JY、PDD、TB、VIP、ELM、DY
     * beginTimeStr：2021-04-29 00:00:00
     * endTimeStr：2021-05-05 23:59:59
     * downTimeType: 京东： 1-下单时间，2-完成时间，3-更新时间
     *               淘宝： 1：按照订单淘客创建时间查询，2:按照订单淘客付款时间查询，3:按照订单淘客结算时间查询，4:按照订单更新时间;
     *           淘宝维权:  1-维权发起时间; 3-维权完成时间，4-订单创建时间，5-订单更新时间
     *             饿了么:  1-付款时间；2-创建时间；3-结算时间；4-更新时间
     *             抖音:    1-付款时间；4-更新时间
     *             唯品会:  1-付款时间；4-更新时间
     */
    @XxlJob("pullOrdersByAppKeyHandler")
    public void pullOrdersByAppKeyHandler() throws Exception {
        String paramStr = "";
        try {
            paramStr = XxlJobHelper.getJobParam();
            if(StringUtils.isNotBlank(paramStr)){
                String[] paraArr = paramStr.split(",");
                if(paraArr != null && paraArr.length >= 3){
                    String type = paraArr[0].toUpperCase();
                    String beginTimeStr = paraArr[1];
                    String endTimeStr = paraArr[2];

                    Integer downTimeType = null;
                    if(paraArr.length > 3){
                        try {
                            downTimeType = Integer.valueOf(paraArr[3]);
                        }catch (Exception e){

                        }
                    }else{
                        if("JD".equals(type)){
                            downTimeType = 3;
                        }else if("TB".equals(type)){
                            downTimeType = 4;
                        }else if("TBWQ".equals(type)){
                            downTimeType = 5;
                        }else if("ELM".equals(type)){
                            downTimeType = 4;
                        }else if("DY".equals(type)){
                            downTimeType = 4;
                        }else if("VIP".equals(type)){
                            downTimeType = 4;
                        }
                    }

                    Long begin_time = Long.valueOf(DateUtil.getTime(DateFormatUtil.StringToDateFormat(beginTimeStr)));
                    Long end_time = Long.valueOf(DateUtil.getTime(DateFormatUtil.StringToDateFormat(endTimeStr)));
                    if("JD".equals(type)){ //京东的订单
                        JdOrderSyncParam jdOrderSyncParam = new JdOrderSyncParam();
                        jdOrderSyncParam.setBegin_time(begin_time);
                        jdOrderSyncParam.setEnd_time(end_time);

                        if(downTimeType == null){
                            downTimeType = 3;
                        }

                        jdOrderSyncParam.setDownTimeType(downTimeType);
                        //jdOrderService.syncOrder_byAppKey(jdOrderSyncParam);
                        //modify on 2021-12-21 修改成将补拉订单放入redis当中
                        jdOrderService.syncOrder_byAppKey_producer(jdOrderSyncParam);
                        logger.info("--------OrderPullByAppKeySyncXxlJob_pullJdOrders------start------"+beginTimeStr+"----"+end_time);
                    }else if("JY".equals(type)){ //京拥的订单
                        JdOrderSyncParam jdOrderSyncParam = new JdOrderSyncParam();
                        jdOrderSyncParam.setBegin_time(begin_time);
                        jdOrderSyncParam.setEnd_time(end_time);
                        jdOrderService.syncJyOrder_byAppKey(jdOrderSyncParam);
                        logger.info("--------OrderPullByAppKeySyncXxlJob_pullJyOrders------start------"+beginTimeStr+"----"+end_time);
                    }else if("PDD".equals(type)){ //平多多订单
                        PddOrderSyncParam pddOrderSyncParam = new PddOrderSyncParam();
                        pddOrderSyncParam.setBegin_time(begin_time);
                        pddOrderSyncParam.setEnd_time(end_time);
                        pddOrderService.syncOrder(pddOrderSyncParam);
                        logger.info("--------OrderPullByAppKeySyncXxlJob_pullPddOrders------start------"+beginTimeStr+"----"+end_time);
                    }else if("TB".equals(type)){ //淘宝订单

                        if(downTimeType == null){
                            downTimeType = 4;
                        }

                        //会员订单
                        TbOrderSyncParam tbOrderSyncParamSld = new TbOrderSyncParam();
                        tbOrderSyncParamSld.setBegin_time(begin_time);
                        tbOrderSyncParamSld.setEnd_time(end_time);
                        tbOrderSyncParamSld.setTimeType(downTimeType); // 1：按照订单淘客创建时间查询，2:按照订单淘客付款时间查询，3:按照订单淘客结算时间查询 4:按照订单更新时间
                        tbOrderSyncParamSld.setOrderScene(3l); //会员
                        tbOrderSyncParamSld.setRedisQueueKey(CacheKey.tbOrderSldQueue);
                        //调用生产者线程，遍历联盟，往redis队列中添加alimmTokens
                        tbOrderService.syncOrder_producer(tbOrderSyncParamSld);

                        //普通订单
                        TbOrderSyncParam tbOrderSyncParam = new TbOrderSyncParam();
                        tbOrderSyncParam.setBegin_time(begin_time);
                        tbOrderSyncParam.setEnd_time(end_time);
                        tbOrderSyncParam.setTimeType(downTimeType); // 1：按照订单淘客创建时间查询，2:按照订单淘客付款时间查询，3:按照订单淘客结算时间查询 4:按照订单更新时间
                        tbOrderSyncParam.setRedisQueueKey(CacheKey.tbOrderQueue);
                        //调用生产者线程，遍历联盟，往redis队列中添加alimmTokens
                        tbOrderService.syncOrder_producer(tbOrderSyncParam);

                        //渠道订单
                        TbOrderSyncParam tbOrderSyncParamChannel = new TbOrderSyncParam();
                        tbOrderSyncParamChannel.setBegin_time(begin_time);
                        tbOrderSyncParamChannel.setEnd_time(end_time);
                        tbOrderSyncParamChannel.setTimeType(downTimeType); // 1：按照订单淘客创建时间查询，2:按照订单淘客付款时间查询，3:按照订单淘客结算时间查询 4:按照订单更新时间
                        tbOrderSyncParamChannel.setOrderScene(2L); //渠道
                        tbOrderSyncParamChannel.setRedisQueueKey(CacheKey.tbOrderChannelQueue);
                        //调用生产者线程，遍历联盟，往redis队列中添加alimmTokens
                        tbOrderService.syncOrder_producer(tbOrderSyncParamChannel);

                        logger.info("--------OrderPullByAppKeySyncXxlJob_pullTBOrders------start------"+beginTimeStr+"----"+end_time);
                    }else if("TBWQ".equals(type)) { //淘宝维权订单

                        if(downTimeType == null){
                            downTimeType = 5;
                        }

                        //淘宝维权渠道订单
                        TbOrderRefundSyncParam tbOrdersRefundSyncParam_channel = new TbOrderRefundSyncParam();
                        tbOrdersRefundSyncParam_channel.setBegin_time(begin_time);
                        tbOrdersRefundSyncParam_channel.setEnd_time(end_time);
                        tbOrdersRefundSyncParam_channel.setType(downTimeType);
                        tbOrdersRefundSyncParam_channel.setBizType(1L);             //1代表渠道订单
                        tbOrdersRefundSyncParam_channel.setRedisQueueKey(CacheKey.tbOrderRefundQueue);
                        //调用生产者线程，遍历联盟，往redis队列中添加alimmTokens
                        tbOrderRefundService.syncOrder_producer(tbOrdersRefundSyncParam_channel);

                        //modify by 20230207 新接口下载是全量下载，不需要分开下载维权订单
                        //淘宝维权会员订单-
//                        TbOrderRefundSyncParam tbOrdersRefundSyncParam_sid = new TbOrderRefundSyncParam();
//                        tbOrdersRefundSyncParam_sid.setBegin_time(begin_time);
//                        tbOrdersRefundSyncParam_sid.setEnd_time(end_time);
//                        tbOrdersRefundSyncParam_sid.setType(downTimeType);
//                        tbOrdersRefundSyncParam_sid.setBizType(2L);             //2代表会员订单
//                        tbOrdersRefundSyncParam_sid.setRedisQueueKey(CacheKey.tbOrderRefundQueue);
//                        //调用生产者线程，遍历联盟，往redis队列中添加alimmTokens
//                        tbOrderRefundService.syncOrder_producer(tbOrdersRefundSyncParam_sid);

                        logger.info("--------OrderPullByAppKeySyncXxlJob_pullTBOrdersRefund------start------"+beginTimeStr+"----"+end_time);

                    }else if("VIP".equals(type)){ //唯品会订单

                        if(downTimeType == null){
                            downTimeType = 4;
                        }

                        //普通订单
                        VipOrderSyncParam vipOrderSyncParam = new VipOrderSyncParam();
                        vipOrderSyncParam.setBegin_time(begin_time * 1000);
                        vipOrderSyncParam.setEnd_time(end_time * 1000);
                        vipOrderSyncParam.setRedisQueueKey(CacheKey.vipPayOrderQueue);
                        vipOrderSyncParam.setDownTimeType(downTimeType);
                        //调用生产者线程，遍历唯品会联盟，往redis队列中添加vipTokens
                        vipOrderService.syncOrder_producer(vipOrderSyncParam);

                        //维权订单
                        VipOrderSyncParam vipOrderSyncParam_Refund = new VipOrderSyncParam();
                        vipOrderSyncParam_Refund.setBegin_time(begin_time * 1000);
                        vipOrderSyncParam_Refund.setEnd_time(end_time * 1000);
                        vipOrderSyncParam_Refund.setDownTimeType(downTimeType);
                        vipOrderSyncParam_Refund.setRedisQueueKey(CacheKey.vipRefundOrderQueue);
                        vipRefundOrderService.syncOrder_producer(vipOrderSyncParam_Refund);

                        logger.info("--------OrderPullByAppKeySyncXxlJob_pullVIPOrders------start------"+beginTimeStr+"----"+end_time);
                    }else if("ELM".equals(type)){ //饿了么订单
                        ElmOrderSyncParam elmOrderSyncParam = new ElmOrderSyncParam();
                        elmOrderSyncParam.setBegin_time(begin_time);
                        elmOrderSyncParam.setEnd_time(end_time);
                        if(downTimeType == null){
                            downTimeType = 4; //更新时间
                        }
                        elmOrderSyncParam.setDownTimeType(downTimeType);
                        elmOrderService.syncOrder_byAppKey_producer(elmOrderSyncParam);
                        logger.info("--------OrderPullByAppKeySyncXxlJob_pullElmOrders------start------"+beginTimeStr+"----"+end_time);
                    }else if("DY".equals(type)){ //抖音订单
                        DyOrderSyncParam dyOrderSyncParyam = new DyOrderSyncParam();
                        dyOrderSyncParyam.setBegin_time(begin_time);
                        dyOrderSyncParyam.setEnd_time(end_time);
                        if(downTimeType == null){
                            downTimeType = 4; //更新时间
                        }
                        dyOrderSyncParyam.setRedisQueueKey(CacheKey.dyPayOrderQueue);
                        dyOrderSyncParyam.setDownTimeType(downTimeType);
                        dyOrderService.syncOrder_producer(dyOrderSyncParyam);
                        logger.info("--------OrderPullByAppKeySyncXxlJob_pullDyOrders------start------"+beginTimeStr+"----"+end_time);
                    }else if("ALL".equals(type)){ //所有的订单
                        //京东
                        JdOrderSyncParam jdOrderSyncParam = new JdOrderSyncParam();
                        jdOrderSyncParam.setBegin_time(begin_time);
                        jdOrderSyncParam.setEnd_time(end_time);
                        jdOrderSyncParam.setDownTimeType(3);
                        //jdOrderService.syncOrder_byAppKey(jdOrderSyncParam);
                        //modify on 2021-12-21 修改成将补拉订单放入redis当中
                        jdOrderService.syncOrder_byAppKey_producer(jdOrderSyncParam);
                        logger.info("--------OrderPullByAppKeySyncXxlJob_pullJdOrders------start------"+beginTimeStr+"----"+end_time);

                        //拼多多
                        PddOrderSyncParam pddOrderSyncParam = new PddOrderSyncParam();
                        pddOrderSyncParam.setBegin_time(begin_time);
                        pddOrderSyncParam.setEnd_time(end_time);
                        pddOrderService.syncOrder(pddOrderSyncParam);
                        logger.info("--------OrderPullByAppKeySyncXxlJob_pullPddOrders------start------"+beginTimeStr+"----"+end_time);

                        //淘宝会员订单
                        TbOrderSyncParam tbOrderSyncParamSld = new TbOrderSyncParam();
                        tbOrderSyncParamSld.setBegin_time(begin_time);
                        tbOrderSyncParamSld.setEnd_time(end_time);
                        tbOrderSyncParamSld.setTimeType(4);        //按照订单更新时间；
                        tbOrderSyncParamSld.setOrderScene(3l); //会员
                        tbOrderSyncParamSld.setRedisQueueKey(CacheKey.tbOrderSldQueue);
                        //调用生产者线程，遍历联盟，往redis队列中添加alimmTokens
                        tbOrderService.syncOrder_producer(tbOrderSyncParamSld);

                        //淘宝普通订单
                        TbOrderSyncParam tbOrderSyncParam = new TbOrderSyncParam();
                        tbOrderSyncParam.setBegin_time(begin_time);
                        tbOrderSyncParam.setEnd_time(end_time);
                        tbOrderSyncParam.setTimeType(4); //按照订单更新时间；
                        tbOrderSyncParam.setRedisQueueKey(CacheKey.tbOrderQueue);
                        //调用生产者线程，遍历联盟，往redis队列中添加alimmTokens
                        tbOrderService.syncOrder_producer(tbOrderSyncParam);

                        //淘宝渠道订单
                        TbOrderSyncParam tbOrderSyncParamChannel = new TbOrderSyncParam();
                        tbOrderSyncParamChannel.setBegin_time(begin_time);
                        tbOrderSyncParamChannel.setEnd_time(end_time);
                        tbOrderSyncParamChannel.setTimeType(4);        //按照订单更新时间；
                        tbOrderSyncParamChannel.setOrderScene(2L); //渠道
                        tbOrderSyncParamChannel.setRedisQueueKey(CacheKey.tbOrderChannelQueue);
                        //调用生产者线程，遍历联盟，往redis队列中添加alimmTokens
                        tbOrderService.syncOrder_producer(tbOrderSyncParamChannel);

                        logger.info("--------OrderPullByAppKeySyncXxlJob_pullTBOrders------start------"+beginTimeStr+"----"+end_time);

                        //淘宝维权渠道订单
                        TbOrderRefundSyncParam tbOrdersRefundSyncParam_channel = new TbOrderRefundSyncParam();
                        tbOrdersRefundSyncParam_channel.setBegin_time(begin_time);
                        tbOrdersRefundSyncParam_channel.setEnd_time(end_time);
                        tbOrdersRefundSyncParam_channel.setType(5);
                        tbOrdersRefundSyncParam_channel.setBizType(1L);             //1代表渠道订单
                        tbOrdersRefundSyncParam_channel.setRedisQueueKey(CacheKey.tbOrderRefundQueue);
                        //调用生产者线程，遍历联盟，往redis队列中添加alimmTokens
                        tbOrderRefundService.syncOrder_producer(tbOrdersRefundSyncParam_channel);

                        //淘宝维权会员订单（采用淘宝维权全量API下载接口，故不需要区分渠道维权和会员维权订单）
//                        TbOrderRefundSyncParam tbOrdersRefundSyncParam_sid = new TbOrderRefundSyncParam();
//                        tbOrdersRefundSyncParam_sid.setBegin_time(begin_time);
//                        tbOrdersRefundSyncParam_sid.setEnd_time(end_time);
//                        tbOrdersRefundSyncParam_sid.setType(5);
//                        tbOrdersRefundSyncParam_sid.setBizType(2L);             //2代表会员订单
//                        tbOrdersRefundSyncParam_sid.setRedisQueueKey(CacheKey.tbOrderRefundQueue);
//                        //调用生产者线程，遍历联盟，往redis队列中添加alimmTokens
//                        tbOrderRefundService.syncOrder_producer(tbOrdersRefundSyncParam_sid);

                        logger.info("--------OrderPullByAppKeySyncXxlJob_pullTBOrdersRefund------start------"+beginTimeStr+"----"+end_time);

                        //唯品会普通订单
                        VipOrderSyncParam vipOrderSyncParam = new VipOrderSyncParam();
                        vipOrderSyncParam.setBegin_time(begin_time * 1000);
                        vipOrderSyncParam.setEnd_time(end_time * 1000);
                        vipOrderSyncParam.setDownTimeType(4); //更新时间
                        vipOrderSyncParam.setRedisQueueKey(CacheKey.vipPayOrderQueue);
                        //调用生产者线程，遍历唯品会联盟，往redis队列中添加vipTokens
                        vipOrderService.syncOrder_producer(vipOrderSyncParam);

                        //唯品会维权订单
                        VipOrderSyncParam vipOrderSyncParam_Refund = new VipOrderSyncParam();
                        vipOrderSyncParam_Refund.setBegin_time(begin_time * 1000);
                        vipOrderSyncParam_Refund.setEnd_time(end_time * 1000);
                        vipOrderSyncParam_Refund.setDownTimeType(4); //更新时间
                        vipOrderSyncParam_Refund.setRedisQueueKey(CacheKey.vipRefundOrderQueue);
                        vipRefundOrderService.syncOrder_producer(vipOrderSyncParam_Refund);

                        logger.info("--------OrderPullByAppKeySyncXxlJob_pullVIPOrders------start------"+beginTimeStr+"----"+end_time);

                        //饿了么
                        ElmOrderSyncParam elmOrderSyncParam = new ElmOrderSyncParam();
                        elmOrderSyncParam.setBegin_time(begin_time);
                        elmOrderSyncParam.setEnd_time(end_time);
                        elmOrderSyncParam.setDownTimeType(4);
                        elmOrderService.syncOrder_byAppKey_producer(elmOrderSyncParam);
                        logger.info("--------OrderPullByAppKeySyncXxlJob_pullElmOrders------start------"+beginTimeStr+"----"+end_time);

                        //抖音
                        DyOrderSyncParam dyOrderSyncParyam = new DyOrderSyncParam();
                        dyOrderSyncParyam.setBegin_time(begin_time);
                        dyOrderSyncParyam.setEnd_time(end_time);
                        dyOrderSyncParyam.setRedisQueueKey(CacheKey.dyPayOrderQueue);
                        dyOrderSyncParyam.setDownTimeType(4);
                        dyOrderService.syncOrder_producer(dyOrderSyncParyam);
                        logger.info("--------OrderPullByAppKeySyncXxlJob_pullDyOrders------start------"+beginTimeStr+"----"+end_time);

                    }else{
                        logger.error("xxl-job para only jd/jy/pdd error {}:"+paramStr);
                    }
                }else{
                    logger.error("xxl-job para set is error {}:"+paramStr);
                }
            }else{
                logger.error("xxl-job para set is null {}:"+paramStr);
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("xxl-job pullOrdersByAppKeyHandler is error {}:");
        }
    }

}
